package com.qffx.moviebase.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Created by lenovo on 2016/12/12.
 */

public class NetWorkUtils {
    //判断是否有网络
    public static boolean isNetWorkAvailable(Context context){
        ConnectivityManager manager= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] infos = manager.getAllNetworkInfo();
        if (infos!=null) {
            for(NetworkInfo info:infos){
                if(info.getState()==NetworkInfo.State.CONNECTED){
                    return true;

                }

            }


        }
        return false;
    }
/**
 * 返回网络类型
 *
 */
   public static NetWorkState getNetWorkState(Context context){

       ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(manager.getActiveNetworkInfo().getType()==ConnectivityManager.TYPE_WIFI){
    return  NetWorkState.WIFI;

}else if(manager.getActiveNetworkInfo().getType()==ConnectivityManager.TYPE_MOBILE){
    return  NetWorkState.MOBILE;

}else {
    return NetWorkState.NONE;

}








   }

    //网络状态的枚举
    public enum  NetWorkState{
    WIFI,MOBILE,NONE
}
//    //获得网络状态,在需要判断网络状况的activity中或是fragement中血药写的方法
//    public void getNetWorkState() {
//        if(NetWorkUtils.isNetWorkAvailable(this)){//有网的情况
//            if(NetWorkUtils.getNetWorkState(this)== NetWorkUtils.NetWorkState.WIFI){
//                Toast.makeText(this, "wifi网络", Toast.LENGTH_SHORT).show();
//
//            }else if(NetWorkUtils.getNetWorkState(this)== NetWorkUtils.NetWorkState.MOBILE)
//                Toast.makeText(this, "数据流量", Toast.LENGTH_SHORT).show();
//
//        }else{//没网的情况
//            Toast.makeText(this, "没有网络,请连接网络", Toast.LENGTH_SHORT).show();
//        }
//
//    }



}

